﻿using System;
using System.Diagnostics;

namespace Tables
{
    class StringBuffer : IDisposable
    {
        public StringBuffer()
            : this(500)
        { }
        public StringBuffer(int maxBuffer)
        {
            _curOffset = 0;
            _maxBuffer = maxBuffer;
            _buf = new string[maxBuffer];
        }
        public bool WillFit(int len)
        {
            return _curOffset + len + 1 < _maxBuffer;
        }
        public void Add(string str)
        {
            // copy string to array, then increment position pointer 
            // past the string and (1) null space
            _buf[_curOffset] = string.Copy(str);
            _curOffset += str.Length + 1;
        }
        public int GetOffset()
        {
            return _curOffset;
        }
        public bool IsEqual(int offString, string str)
        {
            string strStored = _buf[offString];
            //string.compare returns 0 if both strings are equal
            return string.Compare(str, strStored, StringComparison.Ordinal) == 0;
        }
        public string GetString(int offstring)
        {
            return _buf[offstring];
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    //disposed managed resource

                }
                // release unmanaged resource
            }
            _disposed = true;

            // if available, call to base class
            // base.Dispose(disposing);
        }


        private bool _disposed = false;
        private int _maxBuffer;
        private int _curOffset;
        private string[] _buf;
    }
}
